<template>
  <div class="device-container">
    <div class="toolbar">
      <el-button type="primary" @click="handleAdd">
        <el-icon><Plus /></el-icon>新增仪表
      </el-button>
      <el-input
        v-model="searchForm.keyword"
        placeholder="请输入仪表名称或编号"
        class="search-input"
        clearable
        @keyup.enter="handleSearch"
      >
        <template #prefix>
          <el-icon><Search /></el-icon>
        </template>
      </el-input>
    </div>

    <el-table
      v-loading="loading"
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column prop="deviceNo" label="仪表编号" width="120" />
      <el-table-column prop="deviceName" label="仪表名称" width="150" />
      <el-table-column prop="deviceType" label="仪表类型" width="120" />
      <el-table-column prop="location" label="安装位置" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="{ row }">
          <el-tag :type="row.status === 1 ? 'success' : 'danger'">
            {{ row.status === 1 ? '正常' : '异常' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="lastCollectTime" label="最后采集时间" width="180" />
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
          <el-button link type="primary" @click="handleView(row)">查看</el-button>
          <el-button link type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        v-model:current-page="searchForm.page"
        v-model:page-size="searchForm.pageSize"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 编辑/新增弹窗 -->
    <el-dialog
      :title="dialogType === 'add' ? '新增仪表' : '编辑仪表'"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="仪表编号" prop="deviceNo">
          <el-input v-model="form.deviceNo" placeholder="请输入仪表编号" />
        </el-form-item>
        <el-form-item label="仪表名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入仪表名称" />
        </el-form-item>
        <el-form-item label="仪表类型" prop="deviceType">
          <el-select v-model="form.deviceType" placeholder="请选择仪表类型">
            <el-option label="电表" value="electric" />
            <el-option label="水表" value="water" />
            <el-option label="气表" value="gas" />
          </el-select>
        </el-form-item>
        <el-form-item label="安装位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入安装位置" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { Plus, Search } from '@element-plus/icons-vue'
import type { FormInstance } from 'element-plus'

// 搜索表单
const searchForm = reactive({
  keyword: '',
  page: 1,
  pageSize: 10
})

// 表格数据
const loading = ref(false)
const tableData = ref([])
const total = ref(0)

// 弹窗相关
const dialogVisible = ref(false)
const dialogType = ref<'add' | 'edit'>('add')
const formRef = ref<FormInstance>()
const form = reactive({
  deviceNo: '',
  deviceName: '',
  deviceType: '',
  location: ''
})

// 表单校验规则
const rules = {
  deviceNo: [{ required: true, message: '请输入仪表编号', trigger: 'blur' }],
  deviceName: [{ required: true, message: '请输入仪表名称', trigger: 'blur' }],
  deviceType: [{ required: true, message: '请选择仪表类型', trigger: 'change' }],
  location: [{ required: true, message: '请输入安装位置', trigger: 'blur' }]
}

// 搜索
const handleSearch = () => {
  searchForm.page = 1
  loadData()
}

// 加载数据
const loadData = async () => {
  loading.value = true
  try {
    // TODO: 调用接口获取数据
    // const res = await api.getDeviceList(searchForm)
    // tableData.value = res.data.list
    // total.value = res.data.total
  } finally {
    loading.value = false
  }
}

// 分页操作
const handleSizeChange = (val: number) => {
  searchForm.pageSize = val
  loadData()
}

const handleCurrentChange = (val: number) => {
  searchForm.page = val
  loadData()
}

// 新增
const handleAdd = () => {
  dialogType.value = 'add'
  dialogVisible.value = true
  form.deviceNo = ''
  form.deviceName = ''
  form.deviceType = ''
  form.location = ''
}

// 编辑
const handleEdit = (row: any) => {
  dialogType.value = 'edit'
  dialogVisible.value = true
  Object.assign(form, row)
}

// 查看
const handleView = (row: any) => {
  // TODO: 实现查看详情
}

// 删除
const handleDelete = async (row: any) => {
  // TODO: 实现删除
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    // TODO: 调用接口保存数据
    dialogVisible.value = false
    loadData()
  } catch (error) {
    // 表单验证失败
  }
}

// 初始加载
loadData()
</script>

<style lang="scss" scoped>
.device-container {
  .toolbar {
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    
    .search-input {
      width: 300px;
    }
  }
  
  .pagination {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
